package com.yanwq.sayhi.controller.club.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.yanwq.sayhi.R;
import com.yanwq.sayhi.base.BaseToolbarActivity;
import com.yanwq.sayhi.common.client.MImageLoader;
import com.yanwq.sayhi.common.client.OkJsonObjectResponseCallback;
import com.yanwq.sayhi.common.client.OkRequestParams;
import com.yanwq.sayhi.common.client.OkRestClient;
import com.yanwq.sayhi.common.constant.CacheConstant;
import com.yanwq.sayhi.common.tool.CropTool;
import com.yanwq.sayhi.common.tool.DateFormatTool;
import com.yanwq.sayhi.dao.bean.Club;
import com.yanwq.sayhi.widget.InputDialog;
import com.yanwq.sayhi.widget.MToast;
import com.yanwq.sayhi.widget.SelectDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyClubActivity extends BaseToolbarActivity {

    @BindView(R.id.club_avatar)
    ImageView mIVAvatar;
    @BindView(R.id.club_time)
    TextView mTVTime;
    @BindView(R.id.club_current_permission)
    TextView mTVPermission;
    @BindView(R.id.club_name_l)
    View mVName;
    @BindView(R.id.club_name)
    TextView mTVName;
    @BindView(R.id.club_motto)
    TextView mTVMotto;
    @BindView(R.id.club_motto_l)
    View mVMotto;
    @BindView(R.id.club_vip_l)
    View mVVip;
    @BindView(R.id.club_password_l)
    View mVPwd;
    @BindView(R.id.club_permission_l)
    View mVPermission;

    private Club mClub;
    private File mPhotoFile;
    private String mFileName;
    private CropTool mCropTool;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_club);
    }

    @Override
    public void init() {
        mClub = (Club) getIntent().getSerializableExtra("club");
        mFileName = CacheConstant.getRandomFileName();
        mPhotoFile = new File(CacheConstant.getPhotoPath(), mFileName);
        mCropTool = new CropTool();
    }

    @Override
    public void initView() {
        mToolbar.setTitle("我的俱乐部");

        mIVAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCropTool.pick(MyClubActivity.this);
            }
        });

        mVName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyClubActivity.this, ClubNameActivity.class);
                intent.putExtra("club", mClub);
                startActivityForResult(intent, 0xf1);
            }
        });

        mVMotto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyClubActivity.this, ClubMottoActivity.class);
                intent.putExtra("club", mClub);
                startActivityForResult(intent, 0xf1);
            }
        });

        mVVip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyClubActivity.this, ClubVipListActivity.class);
                intent.putExtra("club", mClub);
                startActivity(intent);
            }
        });

        mVPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyClubActivity.this, ClubPwdActivity.class);
                intent.putExtra("club", mClub);
                startActivity(intent);
            }
        });

        mVPermission.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final List<SelectDialog.Item<Integer>> list = new ArrayList<>();
                list.add(new SelectDialog.Item<>("公开", 0));
                list.add(new SelectDialog.Item<>("需要密码", 1));
                list.add(new SelectDialog.Item<>("需要申请", 2));
                SelectDialog<Integer> selectDialog = new SelectDialog<>(MyClubActivity.this, list);
                selectDialog.setOnSelectedListener(new SelectDialog.OnSelectedListener<Integer>() {
                    @Override
                    public void onSelected(String item, Integer integer) {
                        alterClubPermission(integer);
                    }
                });
                selectDialog.show();
            }
        });

        refreshView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mCropTool.crop(mPhotoFile, this, requestCode, resultCode, data, new CropTool.CropCallback() {
            @Override
            public void onComplete(Uri resultUri) {
                mIVAvatar.setImageURI(resultUri);
                uploadPhoto();
            }
        });
    }

    @Override
    public void finish() {
        Intent intent = getIntent();
        intent.putExtra("club", mClub);
        setResult(0xf1, intent);
        super.finish();
    }

    private void refreshView() {
        mTVTime.setText(String.format("创建于 %1$s", DateFormatTool.format(mClub.getCreateTime())));
        mTVPermission.setText(getPermissionName(mClub.getPermission()));
        MImageLoader.displayImage(mClub.getPhotoOriginUrl(), mIVAvatar);
        mTVName.setText(mClub.getName());
        mTVMotto.setText(mClub.getMotto());
    }

    private String getPermissionName(int permission) {
        switch (permission) {
            case 0:
                return "公开";
            case 1:
                return "加入需要密码";
            case 2:
                return "加入需要验证";
        }
        return "";
    }

    private void uploadPhoto() {
        OkRequestParams params = new OkRequestParams();
        params.put("mf", mFileName, mPhotoFile);
        OkRestClient.getInstance().post(this, "/oss/picture", params, new OkJsonObjectResponseCallback() {
            @Override
            public void onStart() {
                showProgressDialog("正在上传头像");
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                alterClubAvatar(jsonObject);
            }

            @Override
            public void onFail(int status, String s, Throwable throwable) {
                super.onFail(status, s, throwable);
                cancelProgressDialog();
            }

            @Override
            public void onStop() {

            }
        });
    }

    private void alterClubAvatar(JSONObject pic) {
        mClub.setPhotoThumbUrl(pic.getString("picThumbUrl"));
        mClub.setPhotoMediumUrl(pic.getString("picMediumUrl"));
        mClub.setPhotoOriginUrl(pic.getString("picOriginUrl"));

        OkRestClient.getInstance().post(this, "/club/update", mClub, new OkJsonObjectResponseCallback() {
            @Override
            public void onStart() {
                showProgressDialog("正在修改头像");
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                MToast.showShort(getApplicationContext(), "新图片真漂亮");
            }

            @Override
            public void onStop() {
                cancelProgressDialog();
            }
        });
    }

    private void alterClubPermission(int permission) {
        mClub.setPermission(permission);

        if (permission == 1) {
            final InputDialog dialog = new InputDialog(this);
            dialog.setPrompt("请输入4位口令");
            dialog.setOkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String pwd = dialog.getContent();
                    if (pwd.length() > 0) {
                        mClub.setPassword(pwd);
                        sendAlterPermission();
                    } else {
                        MToast.showShort(getApplicationContext(), "先输入口令");
                    }
                }
            });
            dialog.show();
            return;
        }

        sendAlterPermission();
    }

    private void sendAlterPermission() {
        OkRestClient.getInstance().post(MyClubActivity.this, "/club/update", mClub, new OkJsonObjectResponseCallback() {
            @Override
            public void onStart() {
                showProgressDialog("正在修改权限");
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                mClub.setPermission(jsonObject.getIntValue("permission"));
                refreshView();
                MToast.showShort(getApplicationContext(), "权限已修改");
            }

            @Override
            public void onStop() {
                cancelProgressDialog();
            }
        });
    }
}
